package cn.seaboot.admin.file.word;

import cn.seaboot.admin.file.resource.ClassPathResourceService;
import cn.seaboot.admin.file.resource.ClassPathResourceProperties;
import cn.seaboot.commons.file.MimeUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;

/**
 * Excel模版操作
 *
 * @author ChenSS
 * @version 2019-04-22 15:26:05
 */
@Service
public class WordTplService {

    @Resource
    private ClassPathResourceService classPathResourceService;

    @Resource
    private ClassPathResourceProperties classPathResourceProperties;

    /**
     * 导出空的Excel模版
     */
    public InputStream openInputSteam(OutputStream os, String tplName) throws IOException {
        return classPathResourceService.openInputSteam(classPathResourceProperties.getWord(), tplName, MimeUtils.DOCX);
    }
}
// E.G.
//
//     /**
//      * 数据写入到Word文档的第一个表格
//      *
//      * @param request  -
//      * @param response -
//      * @param tplName  模版名称，包括后缀
//      * @param chnName  文件导出采用的名称
//      * @throws Exception -
//      */
//     public <T> void writeIntoTable(HttpServletRequest request, HttpServletResponse response, String tplName, String chnName,
//                                    Map<String, Object> params) throws Exception {
//         OutputStream os = ResponseWriter.create(request, response).setDownloadName(chnName).toOutputStream();
//         try (InputStream is = resourceDataService.openInputSteam(resourceConstant.getWord(), tplName, MimeUtils.DOCX);
//              XWPFDocument doc = new XWPFDocument(is)) {
//             XWPFTable table = doc.getTables().get(0);
//
//             // do sth.
//             doc.write(os);
//         }
//     }
//
//     /**
//      * 获取导出图片集合
//      *
//      * @param type   图片类型
//      * @param imgUrl 数据库保存的图片Url字符串
//      */
//     public List<DocPicture> getLocalList(String type, String imgUrl) throws IOException {
//         if (imgUrl != null) {
//             String[] images = imgUrl.split(";");
//             List<DocPicture> pictures = new ArrayList<>(images.length);
//             for (String img : images) {
//                 File file = Uploader.create(type).getFile(img);
//                 DocPicture pic = new DocPicture(IOUtils.openFileInputStream(file), 200, 200);
//                 pictures.add(pic);
//             }
//             return pictures;
//         }
//         return null;
//     }
//
//     /**
//      * 获取导出图片集合
//      */
//     public List<DocPicture> getPictureList(List<MultipartFile> list) throws IOException {
//         if (CommonUtils.isEmpty(list)) {
//             return null;
//         } else {
//             return list.stream().map(file -> {
//                 try {
//                     return new DocPicture(file.getInputStream(), 200, 200);
//                 } catch (IOException ignore) {
//                 }
//                 return null;
//             }).filter(Objects::nonNull).collect(Collectors.toList());
//         }
//     }
